﻿Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Media
Imports System.Windows.Forms.Design

Public Class CTEditorFormV2


    '这个是为了自定义编辑器
    Private m_Provider As IWindowsFormsEditorService


    Public Sub New(ByVal DialogProvider As IWindowsFormsEditorService, ByRef prop As SDSContourLayer)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()



        '为了属性设计器
        m_Provider = DialogProvider

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        mainList.Clear()
        lstContour.Items.Clear()

        cprop = prop


        Dim strVal As String = ""
        For Each t As ContourLine In cprop.CListProperty


            mainList.Add(t.VProperty, t)
            lstContour.Items.Add(t.VProperty.ToString())


            '都是颜色计算惹的祸啊！
            Dim temp As Double = t.VProperty
            If temp < minVal Then

                minVal = temp

            End If

            If temp > maxVal Then
                maxVal = temp

            End If





            '方便用户，给第一个框赋值
            If strVal = "" Then
                strVal = strVal + t.VProperty.ToString()
            Else
                strVal = strVal + "," + t.VProperty.ToString()
            End If


        Next

        txtSelf.Text = strVal



    End Sub


    'Dim mainList As New List(Of ContourSub)
    Dim mainList As New Dictionary(Of Double, ContourLine)
    

    Dim resultList As New List(Of ContourLine)
    Public ReadOnly Property cMList() As List(Of ContourLine)
        Get
            Return resultList
        End Get
    End Property

    Dim nCounts As Integer = 0
    Dim minVal As Double = Double.MaxValue
    Dim maxVal As Double = Double.MinValue




    Dim cprop As SDSContourLayer


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        'Dim temp As New List(Of ContourLine)



        For Each p As KeyValuePair(Of Double, ContourLine) In mainList
            Dim val As ContourLine = p.Value


            resultList.Add(val)


        Next





        Me.Close()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim str As String = txtSelf.Text.Trim()

        Dim lst As New List(Of String)

        Dim nstart As Integer = -1
        Dim nend As Integer = -1

        If str = "" Then
            Return
        End If






        Dim partStr As String

        nstart = 0

        While str.IndexOf(",") <> -1

            nend = str.IndexOf(",")
            partStr = str.Substring(nstart, nend)


            If lst.Contains(partStr) Then

            Else

                If partStr = "" Then
                Else
                    lst.Add(partStr)
                End If

            End If



            nend = nend + 1
            Dim strTemp As String
            Dim nlength As Integer = str.Length - 1

            strTemp = str.Substring(nend)
            str = strTemp

        End While


        '加入最后一次
        nend = str.Length
        partStr = str.Substring(nstart, nend)

        If lst.Contains(partStr) Then

        Else

            If partStr = "" Then
            Else
                lst.Add(partStr)
            End If

        End If



        '排序和去重复
        lst.Sort()

        Dim lstDouble As New List(Of Double)

        For Each s As String In lst

            lstDouble.Add(Double.Parse(s))

        Next
        lstDouble.Sort()





        '清空前次的线条
        lstContour.Items.Clear()
        mainList.Clear()

        '为了讨厌的颜色逻辑，真是可恶，和我想的不一样，也得改啊
        nCounts = lstDouble.Count
        minVal = lstDouble.Item(0)
        maxVal = lstDouble.Item(nCounts - 1)

        For Each s As Double In lstDouble

            lstContour.Items.Add(s.ToString())


        Next


        Dim n As Integer = lstContour.Items.Count
        Dim j As Integer = 0
        For Each it As String In lstContour.Items

            '我靠，自己写的代码，自己疯了，为什么用n-1呢，我真脑残
            Me.addContourSub(Double.Parse(it), j, n - 1)

            j = j + 1

        Next






    End Sub

    Private Sub btnCompute2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute2.Click
        If txtMax.Text = "" Or txtMin.Text = "" Or txtSpace.Text = "" Then
            Return
        End If

     





        Dim min As Double = Double.Parse(txtMin.Text)
        Dim max As Double = Double.Parse(txtMax.Text)


        If min > max Then

            MessageBox.Show("最大值不应小于最小值")
            Return

        End If


        Dim space As Double = Double.Parse(txtSpace.Text)

        If (space > max - min) Then

            MessageBox.Show("间距值过大")
            Return
        End If



        lstContour.Items.Clear()
        mainList.Clear()


        For i As Integer = 0 To Math.Truncate((max - min) / space)
            Dim ddd As Double = min + space * i
            lstContour.Items.Add(ddd.ToString())
        Next
        If lstContour.Items(lstContour.Items.Count - 1).ToString < max Then
            lstContour.Items.Add(max.ToString())
        End If


        '为了讨厌的颜色逻辑，真是可恶，和我想的不一样，也得改啊
        nCounts = 0
        For i As Double = min To max Step space
            nCounts = nCounts + 1
        Next
        minVal = min
        maxVal = max


        'For i As Double = min To max Step space

        '    lstContour.Items.Add(i.ToString())

        'Next



        Dim n As Integer = lstContour.Items.Count
        Dim j As Integer = 0
        For Each it As String In lstContour.Items

            Me.addContourSub(Double.Parse(it), j, n - 1)

            j = j + 1

        Next



    End Sub

    Private Sub txtSelf_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSelf.TextChanged

    End Sub

    Private Sub lstContour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContour.Click

    End Sub

    Private Sub lstContour_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContour.SelectedIndexChanged

        If mainList.ContainsKey(lstContour.SelectedItem) Then

            propSub.SelectedObject = mainList.Item(lstContour.SelectedItem)
        Else

            MessageBox.Show("等值线值需要重新生成")

        End If


    End Sub

    Private Sub addContourSub(ByVal val As Double, ByVal iPos As Double, ByVal iAll As Double)



        If mainList.ContainsKey(val) Then

            MessageBox.Show("值为" + val.ToString() + "的线已经存在！")
            Return
        Else

            Dim c As New ContourLine
            c.LnColorProperty = Me.GetLineColor(val, iPos, iAll)
            c.VProperty = val
            c.ClrProperty = Me.GetPolygonColor(val, iPos, iAll)
            mainList.Add(val, c)

        End If




    End Sub

    Private Function GetLineColor(ByVal val As Double, ByVal iPos As Double, ByVal iAll As Double) As Color



        Dim n As Integer = cprop.LineGradientListProperty.Count

        If n < 1 Then
            Return Nothing
        End If

        Dim clrTemp As New List(Of Color)

        If n > 1 Then
            Dim a As Color
            Dim b As Color

            Dim temp As Color

            For i As Integer = 0 To n - 2
                a = cprop.LineGradientListProperty.Item(i)
                b = cprop.LineGradientListProperty.Item(i + 1)

                For j As Double = 1 To 1000

                    Dim atempr As Double = a.R
                    Dim atempg As Double = a.G
                    Dim atempb As Double = a.B

                    Dim btempr As Double = b.R
                    Dim btempg As Double = b.G
                    Dim btempb As Double = b.B


                    Dim rc As Double = atempr + (btempr - atempr) * j / 1000D
                    Dim gc As Double = atempg + (btempg - atempg) * j / 1000D
                    Dim bc As Double = atempb + (btempb - atempb) * j / 1000D

                    temp = Color.FromArgb(rc, gc, bc)

                    clrTemp.Add(temp)


                Next


            Next

        Else
            clrTemp.Add(cprop.LineGradientListProperty.Item(0))


        End If

        Dim nAll As Integer = clrTemp.Count


        Dim index As Integer

        If nAll = 1 Then
            index = 0
        Else

            If (minVal <> maxVal) Then
                ' index = nAll * (val - minVal) / (maxVal - minVal) - 1

                index = nAll * iPos / iAll - 1
            Else
                index = 0
            End If


        End If

        If index > nAll - 1 Then
            index = nAll - 1
        End If

        If index < 0 Then
            index = 0
        End If



        Return clrTemp.Item(index)




    End Function

    Private Function GetPolygonColor(ByVal val As Double, ByVal iPos As Double, ByVal iAll As Double) As Color


        Dim n As Integer = cprop.PolyGradientListProperty.Count

        If n < 1 Then
            Return Nothing
        End If

        Dim clrTemp As New List(Of Color)

        If n > 1 Then
            Dim a As Color
            Dim b As Color

            Dim temp As Color

            For i As Integer = 0 To n - 2
                a = cprop.PolyGradientListProperty.Item(i)
                b = cprop.PolyGradientListProperty.Item(i + 1)

                For j As Double = 1 To 1000

                    Dim atempr As Double = a.R
                    Dim atempg As Double = a.G
                    Dim atempb As Double = a.B

                    Dim btempr As Double = b.R
                    Dim btempg As Double = b.G
                    Dim btempb As Double = b.B


                    Dim rc As Double = atempr + (btempr - atempr) * j / 1000D
                    Dim gc As Double = atempg + (btempg - atempg) * j / 1000D
                    Dim bc As Double = atempb + (btempb - atempb) * j / 1000D

                    temp = Color.FromArgb(rc, gc, bc)

                    clrTemp.Add(temp)


                Next


            Next

        Else
            clrTemp.Add(cprop.PolyGradientListProperty.Item(0))


        End If

        Dim nAll As Integer = clrTemp.Count

        '颜色的返回逻辑，我也很郁闷
        Dim index As Integer


        If nAll = 1 Then
            index = 0
        Else

            If (minVal <> maxVal) Then
                'index = nAll * (val - minVal) / (maxVal - minVal) - 1

                index = nAll * iPos / iAll - 1
            Else
                index = 0
            End If


        End If

        If index > nAll - 1 Then
            index = nAll - 1
        End If

        If index < 0 Then
            index = 0
        End If

        Return clrTemp.Item(index)


    End Function


    Private Sub txtMin_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMin.KeyPress



        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case Else
                e.Handled = True

        End Select


    End Sub

    Private Sub txtMax_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMax.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case Else
                e.Handled = True

        End Select
    End Sub

    Private Sub txtSpace_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSpace.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case Else
                e.Handled = True

        End Select
    End Sub

    Private Sub txtSelf_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSelf.KeyPress
        Select Case e.KeyChar
            Case "0" To "9"
            Case "."
            Case ""
            Case ","


            Case Else
                e.Handled = True

        End Select
    End Sub

    Private Sub btnLineClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLineClr.Click

        Dim n As Integer = mainList.Count
        Dim j As Integer = 0
        For Each p As KeyValuePair(Of Double, ContourLine) In mainList
            Dim k As Double = p.Key
            Dim v As ContourLine = p.Value

            v.LnColorProperty = Me.GetLineColor(k, j, n - 1)
            j = j + 1


        Next

    End Sub

    Private Sub btnPolyClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPolyClr.Click

        Dim n As Integer = mainList.Count
        Dim j As Integer = 0

        For Each p As KeyValuePair(Of Double, ContourLine) In mainList
            Dim k As Double = p.Key
            Dim v As ContourLine = p.Value

            v.ClrProperty = Me.GetPolygonColor(k, j, n - 1)
            j = j + 1


        Next
    End Sub

End Class